package com.cyc.basic.controller;


import com.cyc.basic.pojo.entiy.SysUser;
import com.cyc.basic.pojo.vo.SysUserAddReq;
import com.cyc.basic.pojo.vo.UserReq;
import com.cyc.basic.pojo.vo.UserSaveReq;
import com.cyc.basic.service.user.ISysUserService;
import com.cyc.common.model.ApiResult;
import com.cyc.common.model.Result;
import com.cyc.common.util.BeanHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author chenyunchang
 * @since 2021-01-07
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation("查找用户")
    @PostMapping("/list")
    public Result<List<SysUser>> list(UserReq userReq) {
        List<SysUser> list = sysUserService.list();
        return Result.success(list);
    }

    @ApiOperation("测试添加用户")
    @PostMapping("/add")
    public Result<Void> add(@Valid @RequestBody SysUserAddReq addReq) {
        SysUser sysUser = BeanHelper.copyProperties(addReq, SysUser.class);
        sysUserService.save(sysUser);
        return Result.success();
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public Result<Void> delete(Integer userId) {
        sysUserService.removeById(userId);
        return Result.success();
    }

    @ApiOperation("测试filter")
    @PostMapping("/testFilter")
    public Result<List<SysUser>> testFilter() {
        List<SysUser> sysUserList = sysUserService.testFilter();
        return Result.success(sysUserList);
    }

    @ApiOperation("测试queryWrapper的and和or")
    @PostMapping("/testQueryWrapper")
    public Result<List<SysUser>> testQueryWrapper() {
        List<SysUser> sysUserList = sysUserService.testQueryWrapper();
        return Result.success(sysUserList);
    }

    @ApiOperation("测试spring boot校验参数")
    @PostMapping("/save")
    public ApiResult<List<SysUser>> saveUser(@Valid @RequestBody UserSaveReq userSaveReq) {
        log.info("测试spring boot校验参数");
        sysUserService.saveUser(userSaveReq);
        return ApiResult.success();
    }

    @ApiOperation("枚举类型在sql xml中")
    @PostMapping("/testEnum")
    public ApiResult<Void> saveUser(@RequestBody UserReq userReq) {
        sysUserService.testEnum(userReq);
        return ApiResult.success();
    }
}

